set matsize 11000  

* ) Insert directory
cd ""
use "Author-paper-referee level data", clear

global authorcontrols female gender_missing top10school USNEWS_2 USNEWS_3 USNEWS_5 IDEAS_1 IDEAS_2 IDEAS_3 IDEAS_5 phdyear phdmissing yearsPhD_missing 

* ) Author level summary stats
preserve 
bysort author_id: keep if _n==1
eststo clear
estpost sum $authorcontrols
eststo
restore

* ) Non-desk rejected authors
preserve 
keep if deskreject==0
bysort author_id: keep if _n==1
estpost sum $authorcontrols
eststo
restore

* ) Referee level summary stats
preserve
drop if deskreject==1
bysort referee_id: keep if _n==1
drop $authorcontrols
rename referee_Female female
rename referee_gender_missing gender_missing
rename referee_top10school top10school
rename referee_USNEWS_2 USNEWS_2
rename referee_USNEWS_3 USNEWS_3
rename referee_USNEWS_5 USNEWS_5
rename referee_IDEAS_1 IDEAS_1
rename referee_IDEAS_2 IDEAS_2
rename referee_IDEAS_3 IDEAS_3
rename referee_IDEAS_5 IDEAS_5
rename referee_PhDyear phdyear
rename referee_yearsPhD_missing yearsPhD_missing
rename referee_phdmissing phdmissing

estpost sum $authorcontrols
eststo
*Label variables
label var female "Female"
label var gender_missing "Gender missing"
label var top10school "Institution of PhD (US News): \\ \ \ -Ranked top 10"
label var USNEWS_2 "\ \ -Ranked 11-30"
label var USNEWS_3 "\ \ -Ranked 31-50"
label var USNEWS_5 "\ \ -Ranked 51+ / missing"
label var IDEAS_1 "Institution of PhD (IDEAS): \\ \ \ -Ranked top 10"
label var IDEAS_2 "\ \ -Ranked 11-30"
label var IDEAS_3 "\ \ -Ranked 31-50"
label var IDEAS_5 "\ \ -Ranked 51+/ missing"
label var phdyear "Year receive PhD"
label var yearsPhD_missing "Unknown PhD year"
label var phdmissing "Unknown PhD"

esttab using "summary.tex", cell("mean(label(Mean) fmt(%9.2f)) sd(label(SD))") ///
    replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes nogaps ///
	mgroups("All authors" "\begin{tabular}{@{}c@{}} Passed the desk \\ authors \end{tabular}" "All reviewers", pattern(1 1 1 0 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	nomtitles label postfoot("")
restore

***************************
***************************

* ) Time varying characteristics
* ) Author-paper level summary stats
preserve 
bysort author_id ms_number_id: keep if _n==1
eststo clear
estpost sum published_papers published_from_5_no_pp is_nber degree_1 degree_2 employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing
eststo
restore

* ) Non-desk rejected author-papers
preserve 
keep if deskreject==0
bysort author_id ms_number_id: keep if _n==1
estpost sum published_papers published_from_5_no_pp is_nber degree_1 degree_2 employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing
eststo
restore

* ) Referee-paper level summary stats
preserve
drop if deskreject==1
bysort referee_id ms_number_id: keep if _n==1
drop published_papers published_from_5_no_pp is_nber employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing degree_1 degree_2 
rename referee_published_papers published_papers 
rename referee_published_from_5_no_pp published_from_5_no_pp 
rename referee_is_nber is_nber
rename referee_employmentrank1 employmentrank1
rename referee_employmentrank2 employmentrank2
rename referee_employmentrank3 employmentrank3
rename referee_employmentrank4 employmentrank4
rename referee_employmentrank5 employmentrank5
rename referee_employmentmissing employmentmissing
rename referee_degree_1 degree_1
rename referee_degree_2 degree_2
estpost sum published_papers published_from_5_no_pp is_nber degree_1 degree_2 employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing
eststo

label var published_papers "\# prior publications"
label var published_from_5_no_pp "\# prior top fives"
label var is_nber "NBER affiliated"
label var employmentrank1  "Department rank (IDEAS): \\ \ \ -Ranked top 10"
label var employmentrank2 "\ \ -Ranked 11-30"
label var employmentrank3 "\ \ -Ranked 31-100"
label var employmentrank4 "\ \ -Ranked 101-250"
label var employmentrank5 "\ \ -251+ / missing / non-academic"
label var employmentmissing "\ \ Unknown employment"
label var degree_1 "\# of unique coauthors"
label var degree_2 "\# of coauthors' coauthors"

esttab using "summary.tex", cell("mean(label(Mean) fmt(%9.2f)) sd(label(SD))") ///
    append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes nogaps ///
	mgroups("Author-papers" "\begin{tabular}{@{}c@{}} Passed the desk \\ author-papers \end{tabular}" "Reviewer-papers", pattern(1 1 1 0 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	nomtitles label prehead(" \midrule ") 
restore

***************************
***************************

* ) Variables in table
global vars editor_distance_raw1-editor_distance_raw7 editor_phdmatch editor_USNEWS_match_1 editor_USNEWS_match_2 editor_USNEWS_match_3 editor_USNEWS_match_5 ///
	editor_employmentmatch editor_employmentrankmatch1 editor_employmentrankmatch2 editor_employmentrankmatch3 editor_employmentrankmatch4 editor_employmentrankmatch5 ///
	editor_gendermatch editor_grouptop5match editor_isnbermatch editor_isnbermatch_program

*) When doing editor analysis, don't double count observations due to referee assignment
bysort ms_number_id editor_id author_id: gen indicate = 1 if _n==1

* ) Author-editor-paper level
eststo clear
estpost sum passed_the_desk $vars if indicate==1
eststo

preserve
drop passed_the_desk $vars
*Just to line them up in the same rows
rename RR passed_the_desk
rename referee_distance_raw1 editor_distance_raw1
rename referee_distance_raw2 editor_distance_raw2 
rename referee_distance_raw3 editor_distance_raw3 
rename referee_distance_raw4 editor_distance_raw4 
rename referee_distance_raw5 editor_distance_raw5 
rename referee_distance_raw6 editor_distance_raw6 
rename referee_distance_raw7 editor_distance_raw7 
rename referee_phdmatch editor_phdmatch 
rename referee_USNEWS_match_1 editor_USNEWS_match_1 
rename referee_USNEWS_match_2 editor_USNEWS_match_2 
rename referee_USNEWS_match_3 editor_USNEWS_match_3 
rename referee_USNEWS_match_5 editor_USNEWS_match_5 
rename referee_employmentmatch editor_employmentmatch 
rename referee_employmentrankmatch1 editor_employmentrankmatch1 
rename referee_employmentrankmatch2 editor_employmentrankmatch2 
rename referee_employmentrankmatch3 editor_employmentrankmatch3 
rename referee_employmentrankmatch4 editor_employmentrankmatch4 
rename referee_employmentrankmatch5 editor_employmentrankmatch5
rename gendermatch editor_gendermatch 
rename referee_grouptop5match editor_grouptop5match 
rename referee_isnbermatch editor_isnbermatch 
rename referee_isnbermatch_program editor_isnbermatch_program

estpost sum passed_the_desk $vars if referee_id!=.
eststo

label var passed_the_desk "Outcome: Passed the desk (editor), \\ \ \ \ \ positive evaluation (reviewer)"
label var editor_phdmatch "\underline{Author-editor/reviewer PhD (US News)}: \\ \ \ -Exact same PhD"
label var editor_USNEWS_match_1 "\ \ -Both ranked top 10"
label var editor_USNEWS_match_2 "\ \ -Both ranked 11-30"
label var editor_USNEWS_match_3 "\ \ -Both ranked 31-50"
label var editor_USNEWS_match_5 "\ \ -Both ranked 51+ / missing"
label var editor_distance_raw1 "\underline{Degrees of separation (RePEc)}: \\ \ \ -One (direct coauthors)"
label var editor_distance_raw2 "\ \ -Two (coauthor's coauthor)"
label var editor_distance_raw3 "\ \ -Three"
label var editor_distance_raw4 "\ \ -Four"
label var editor_distance_raw5 "\ \ -Five"
label var editor_distance_raw6 "\ \ -Six+ or no path"
label var editor_distance_raw7 "\ \ -Author and/or reviewer missing"
label var editor_gendermatch "\underline{Author and editor/reviewer both}: \\ \ \ -Female"
label var editor_grouptop5match "\ \ -Published in top five"
label var editor_isnbermatch "\ \ -NBER affiliated" 
label var editor_isnbermatch_program "\ \ -Same NBER program(s)"
label var editor_employmentmatch "\underline{Author-editor/reviewer employment}: \\ \ \ -Former/current colleagues"
label var editor_employmentrankmatch1 "\ \ -Both top 10 department"
label var editor_employmentrankmatch2 "\ \ -Both 11-30 department"
label var editor_employmentrankmatch3 "\ \ -Both 31-100 department"
label var editor_employmentrankmatch4 "\ \ -Both 101-250 department"
label var editor_employmentrankmatch5 "\ \ -251+ / missing / non-academic"

esttab using "summary_obsunit.tex", cell("mean(label(Mean) fmt(%9.2f)) sd(label(SD))") ///
    replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes nogaps ///
	mgroups("Author-editor-papers" "Author-reviewer-papers", pattern(1 1 1 0 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	nomtitles label 
restore

***************************
***************************

* ) Characteristics of those authors/reviewers who get dropped (i.e. singleton observations)
*Get sample to only include those where FE are estimated
eststo: reghdfe RR referee_groupUSNEWSrankmatch, absorb(referee_id ms_number_id) vce(cluster ms_number_id) residuals(resid)
*drop if resid==.

* ) This analysis is only for those that received referee reports
keep if deskreject==0

eststo clear
* ) Author level summary stats
preserve 
bysort author_id: keep if _n==1
estpost sum $authorcontrols if resid!=.
eststo
*Authors dropped due to FE
estpost sum $authorcontrols if resid==.
eststo
restore

* ) Referee level summary stats
preserve
drop if deskreject==1
bysort referee_id: keep if _n==1
drop $authorcontrols
rename referee_Female female
rename referee_gender_missing gender_missing
rename referee_top10school top10school
rename referee_USNEWS_2 USNEWS_2
rename referee_USNEWS_3 USNEWS_3
rename referee_USNEWS_5 USNEWS_5
rename referee_IDEAS_1 IDEAS_1
rename referee_IDEAS_2 IDEAS_2
rename referee_IDEAS_3 IDEAS_3
rename referee_IDEAS_5 IDEAS_5
rename referee_PhDyear phdyear
rename referee_yearsPhD_missing yearsPhD_missing
rename referee_phdmissing phdmissing

estpost sum $authorcontrols if resid!=.
eststo
*Referee dropped due to FE
estpost sum $authorcontrols if resid==.
eststo

*Label variables
label var female "Female"
label var gender_missing "Gender missing"
label var top10school "Institution of PhD (US News): \\ \ \ -Ranked top 10"
label var USNEWS_2 "\ \ -Ranked 11-30"
label var USNEWS_3 "\ \ -Ranked 31-50"
label var USNEWS_5 "\ \ -Ranked 51+ / missing"
label var IDEAS_1 "Institution of PhD (IDEAS): \\ \ \ -Ranked top 10"
label var IDEAS_2 "\ \ -Ranked 11-30"
label var IDEAS_3 "\ \ -Ranked 31-50"
label var IDEAS_5 "\ \ -Ranked 51+ / missing"
label var phdyear "Year receive PhD"
label var yearsPhD_missing "Unknown PhD year"
label var phdmissing "Unknown PhD"

esttab using "summary_dropped.tex", cell("mean(label(Mean) fmt(%9.2f)) sd(label(SD))") ///
    replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes nogaps ///
	mgroups("Kept authors" "Dropped authors" "Kept reviewers" "Dropped reviewers", pattern(1 1 1 1 0 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	nomtitles label postfoot("")
restore

***************************
***************************

* ) Time varying characteristics
* ) Author-paper level summary stats
eststo clear
preserve 
bysort author_id ms_number_id: keep if _n==1
estpost sum published_papers published_from_5_no_pp is_nber degree_1 degree_2 employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing if resid!=. 
eststo
estpost sum published_papers published_from_5_no_pp is_nber degree_1 degree_2 employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing if resid==. 
eststo
restore

* ) Referee-paper level summary stats
preserve
drop if deskreject==1
bysort referee_id ms_number_id: keep if _n==1
drop published_papers published_from_5_no_pp is_nber degree_1 degree_2 employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing
rename referee_published_papers published_papers 
rename referee_published_from_5_no_pp published_from_5_no_pp 
rename referee_is_nber is_nber
rename referee_employmentrank1 employmentrank1
rename referee_employmentrank2 employmentrank2
rename referee_employmentrank3 employmentrank3
rename referee_employmentrank4 employmentrank4
rename referee_employmentrank5 employmentrank5
rename referee_employmentmissing employmentmissing
rename referee_degree_1 degree_1
rename referee_degree_2 degree_2

estpost sum published_papers published_from_5_no_pp is_nber degree_1 degree_2 employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing if resid!=. 
eststo
estpost sum published_papers published_from_5_no_pp is_nber degree_1 degree_2 employmentrank1 employmentrank2 employmentrank3 employmentrank4 employmentrank5 employmentmissing if resid==. 
eststo

label var published_papers "\# prior publications"
label var published_from_5_no_pp "\# prior top fives"
label var is_nber "NBER affiliated"
label var employmentrank1  "Department rank (IDEAS): \\ \ \ -Ranked top 10"
label var employmentrank2 "\ \ -Ranked 11-30"
label var employmentrank3 "\ \ -Ranked 31-100"
label var employmentrank4 "\ \ -Ranked 101-250"
label var employmentrank5 "\ \ -251+ / missing / non-academic"
label var employmentmissing "\ \ Unknown employment"
label var degree_1 "\# of unique coauthors"
label var degree_2 "\# of coauthors' coauthors"

esttab using "summary_dropped.tex", cell("mean(label(Mean) fmt(%9.2f)) sd(label(SD))") ///
    append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) nonum nonotes nogaps ///
	mgroups("\begin{tabular}{@{}c@{}} Kept \\ author-papers \end{tabular}" "\begin{tabular}{@{}c@{}} Dropped \\ author-papers \end{tabular}" "\begin{tabular}{@{}c@{}} Kept \\ reviewer-papers \end{tabular}" "\begin{tabular}{@{}c@{}} Dropped \\ reviewer-papers \end{tabular}", pattern(1 1 1 1 0 0 0 0 ) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  ///
	nomtitles label prehead(" \midrule ") 
restore
